package com.x.example.page;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.Nullable;

import com.x.uikit.page.XPage;
import com.x.uikit.page.XPageManager;
import com.x.uikit.page.XPageManagerConfig;


public class PageActivity extends Activity {
    XPageManager mPageManager;
    PagesRouteHandler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        XPage page = new XPage(this);
        setContentView(page);
        XPageManagerConfig config = XPageManagerConfig.obtainBuilder()
                .setActivity(this)
                .build();
        mPageManager = new XPageManager(config);
        handler = new PagesRouteHandler(mPageManager);
        handler.handRoute(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handler.handRoute(intent);
    }


    @Override
    protected void onStart() {
        super.onStart();
        mPageManager.getLifecycle().onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPageManager.getLifecycle().onResume();
    }

    @Override
    protected void onPause() {
        mPageManager.getLifecycle().onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        mPageManager.getLifecycle().onStop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mPageManager.getLifecycle().onDestroy();
        super.onDestroy();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mPageManager.dispatchKeyEvent(event)) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onBackPressed() {
        boolean handle = mPageManager.onBackPressed();
        if (!handle) {
            super.onBackPressed();
        }
    }
}
